﻿using UnityEngine;
using System.Collections;

public class TableDLCManager : MonoBehaviour {
    public static bool isDone = false;
    public static Hashtable remoteFileVersions = new Hashtable();
    Hashtable localFileVersions = new Hashtable();

    public UISlider dataLoadingProgressBar;

	// Use this for initialization
	void Start () {
#if UNITY_EDITOR
        //StartCoroutine("Co_DownloadFileVersionList");
#endif
	}
	
	// Update is called once per frame
	void Update () {
	
	}

    public void DownloadFileVersionList()
    {
        StartCoroutine("Co_DownloadFileVersionList");
    }

    IEnumerator Co_DownloadFileVersionList()
    {
        Debug.Log("DownloadFileVersionList - ");
        WWW www = new WWW(CSceneManager.WEB_DOWNLOAD_URL + "/GetDataFileVersion.php");

        yield return www;

        if (www.error == null)
        {
            Debug.Log("FileVersionList : " + www.text);
            CheckAndSaveFileVersionList(www.text);
        }
        else
        {
            Debug.Log("error : " + www.error);
        }
    }

    void CheckAndSaveFileVersionList(string fileContents)
    {
        ArrayList downLoadList = new ArrayList();
        string fileName = "DataFileVersionList.txt";
		string FN = string.Format("{0}/{1}", SystemCore.GetAssetPath()+"/UCUpdater/", fileName);
        remoteFileVersions = JSON.JsonDecode(fileContents) as Hashtable;
        Tables.dataFileList = new Hashtable(remoteFileVersions);
        if (LCFileManager.FileExists(FN))
        {
            string[] versionListText = LCFileManager.FileReadAllLine(FN);
            localFileVersions = JSON.JsonDecode(versionListText[0]) as Hashtable;

            foreach (string key in remoteFileVersions.Keys)
            {
				string FN2 = string.Format("{0}/{1}", SystemCore.GetAssetPath()+"/UCUpdater/", key + ".xml");
                if (!LCFileManager.FileExists(FN2))
                {
                    if( downLoadList.IndexOf(key) == -1 )
                        downLoadList.Add(key); 
                }
            }

            foreach (string key in remoteFileVersions.Keys)
            {
                if (!localFileVersions.ContainsKey(key))
                {
                    if (downLoadList.IndexOf(key) == -1)
                        downLoadList.Add(key);
                }
                else
                {
                    if (!remoteFileVersions[key].Equals(localFileVersions[key]))
                    {
                        if (downLoadList.IndexOf(key) == -1)
                            downLoadList.Add(key);
                    }
                }
            }
            if (downLoadList.Count != 0)
            {
                StartDownloadTables(downLoadList);
                LCFileManager.FileWriteAllText(FN, fileContents);
            }
            else
            {
                //LoadingSceneManager.SendVersionCheckPacket();
				StartCoroutine("SendVersionCheckPacket");
                Tables.LoadTable();
            }
        }
        else
        {
            LCFileManager.FileWriteAllText(FN, fileContents);
            StartDownloadTables();
        }
    }

	public IEnumerator SendVersionCheckPacket()
	{
		return LoadingSceneManager.singleton.SendVersionCheckPacket();
	}

    public void StartDownloadTables()
    {
		Tables.isLoadDone = false;
        ArrayList downLoadList = new ArrayList();
        foreach (string key in remoteFileVersions.Keys)
            downLoadList.Add(key);

        StartDownloadTables(downLoadList);
    }

    public void StartDownloadTables(ArrayList downLoadList)
    {
        StartCoroutine(Co_StartDownloadTables(downLoadList));
    }

    IEnumerator Co_StartDownloadTables(ArrayList downLoadList)
    {
        dataLoadingProgressBar.gameObject.SetActive(true);
        Debug.Log("Co_StartDownloadTables - " + downLoadList.Count);
        int currentCount = 0;
        foreach (string key in downLoadList)
        {
            WWW www = new WWW(CSceneManager.WEB_DOWNLOAD_URL + "/" + key + ".xml");

            yield return www;

            if (www.error == null)
            {
                string fileName = key + ".xml";
				string FN = string.Format("{0}/{1}", SystemCore.GetAssetPath()+"/UCUpdater/", fileName);
                LCFileManager.FileWriteAllText(FN, www.text);

                currentCount++;
                dataLoadingProgressBar.value = (float)currentCount / downLoadList.Count;
            }
            else
            {
            }
        }

        dataLoadingProgressBar.gameObject.SetActive(false);

        //LoadingSceneManager.SendVersionCheckPacket();
		StartCoroutine("SendVersionCheckPacket");
        Tables.LoadTable();
    }
}
